package org.vestbjergbyggecenter.Model;

import java.util.ArrayList;
import java.util.Iterator;

public class CustomerRecords {

	private ArrayList<Customer> customers;
	private static CustomerRecords instance=null;
	
	//constructor
	public CustomerRecords()
	{
		customers=new ArrayList<Customer>();
	}
 
	//Method for the instance creation.	
	public static CustomerRecords getInstance()
	{
		if(instance==null)
		{
			instance=new CustomerRecords();
		}
		return instance;
	}
	
	public ArrayList<Customer> getCustomers()
	 {
	  return customers;
	 }
	
	public Customer getCustomer(int id)
	{
		Customer wantedCustomer=null;
		boolean found = false;
        Iterator<Customer> it = customers.iterator();
        while(it.hasNext() && !found)
        {
            Customer customer = it.next();
            if(customer.getID() == id)
            {
                wantedCustomer = customer;
                found = true;
            }
        } 
		return wantedCustomer;
	}
 
	public void removeCustomer(int id)
	{
		if(getCustomer(id)==null)
		{
			System.out.println("Please insert a valid customer ID");
		}
		else
		{
			customers.remove(getCustomer(id));	
		}		
	}
	
  
	public void addCustomer(Customer customer)
	{
		customers.add(customer);
	}

	 public void updateCustomer(int id, String name,long cPR, String address,
				long postalCode, String city, String phone, Category category)
	  {
		 Customer customer = getCustomer(id);
		 customer.setName(name);
		 customer.setAddress(address);
		 customer.setCPR(cPR);
		 customer.setCity(city);
		 customer.setPostalCode(postalCode);
		 customer.setPhone(phone);
		 customer.setCategory(category);
	  }
	  
	//Method to see the elements of the customers Array List.
	 public void printCustomers()
	 {
		 System.out.println("ID   |  Name    |   Phone    |    Address   | Postal Code |   City   |   CPR   |   Category   |");
	        
		 for(Customer customer : customers)
	        {
	            customer.printCustomer();
	        }
	 }
	 
}
